// ============================== VUE配置文件 =============================

'use strict'
const path = require('path');
const defaultSettings = require('./src/settings.js');

function resolve(dir) {
  return path.join(__dirname, dir);
}

const name = defaultSettings.title || '设备组态流程组件';               // 页面标题
const port = process.env.port || process.env.npm_config_port || 9527;  // 本地运行占用端口

module.exports = {
  publicPath: '/',
  outputDir: 'dist',
  assetsDir: 'static',
  lintOnSave: process.env.NODE_ENV === 'development',
  productionSourceMap: false,
  devServer: {
    port: port,
    open: true,
    overlay: {
      warnings: false,
      errors: true
    },
    headers: {
      "Access-Control-Allow-Origin": "*", // 设置为 * 表示接受任何来源的请求
      "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS" // 支持的HTTP方法
    },
    //before: require('./mock/mock-server.js')
    proxy: {
      [process.env.VUE_APP_BASE_API]: {
        target: "http://localhost:9501/",
        changeOrigin: true
      },
      '/dialogue': {
        target: "http://matterai.e3.luyouxia.net:11620/",
        changeOrigin: true,        // 是否跨域
        pathRewrite: {             // 是否重写路径，看代理前端路径是否与后端路径一致
          '^/dialogue':'',         // 将所有含/api路径的，去掉/api转发给服务器
        }
      },
    }
  },
  configureWebpack: {
    // 开启CDN加速：引用cdn下载额外的插件
    externals: {
      'vue': 'Vue',
      'vue-router': 'VueRouter',  // VueRouter
      'vuex': 'Vuex',
      // 'axios': 'axios',
      'element-ui': 'ELEMENT',    // Element
      'echarts': 'echarts',
      'echarts-gl':'echarts-gl',
    },
    name: name,
    resolve: {
      alias: {
        '@': resolve('src')
      }
    }
  },
  chainWebpack(config) {
    config.plugin('preload').tap(() => [{
      rel: 'preload',
      fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
      include: 'initial'
    }])

    // 当有很多页面时，会导致太多无意义的请求
    config.plugins.delete('prefetch')

    // 设置svg精灵加载程序
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()

    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end();

    config
      .when(process.env.NODE_ENV !== 'development',
        config => {
          config
            .plugin('ScriptExtHtmlWebpackPlugin')
            .after('html')
            .use('script-ext-html-webpack-plugin', [{
              // `runtime`必须与runtimeChunk名称相同。默认为`runtime`
              inline: /runtime\..*\.js$/
            }])
            .end();
          config
            .optimization.splitChunks({
              chunks: 'all',
              cacheGroups: {
                libs: {
                  name: 'chunk-libs',
                  test: /[\\/]node_modules[\\/]/,
                  priority: 10,
                  chunks: 'initial'                                 // 仅打包最初依赖的第三方
                },
                elementUI: {
                  name: 'chunk-elementUI',                          // 将elementUI拆分为单个包
                  priority: 20,                                     // 权重需要大于libs和app，否则将被打包到libs或app中
                  test: /[\\/]node_modules[\\/]_?element-ui(.*)/    // 为了适应cnpm
                },
                commons: {
                  name: 'chunk-commons',
                  test: resolve('src/components'),                  // 可以自定义规则
                  minChunks: 3,                                     // 最小公共数
                  priority: 5,
                  reuseExistingChunk: true
                }
              }
            })
          // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
          config.optimization.runtimeChunk('single');
        }
      )
  }
}
